#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 26 * 26 * 26 * 10 + 10;
int getid(char *name)
{
    int id = 0;
    for (int i = 0; i < 3; i++)
        id = 26 * id + (name[i] - 'A');
    id = id * 10 + (name[3] - '0');
    return id;
}
int main()
{
    int n, k, idx, nidx, id;
    char name[5];
    vector<int> v[maxn];
    scanf("%d%d", &n, &k);
    for (int i = 0; i < k; i++)
    {
        scanf("%d%d", &idx, &nidx);
        for (int j = 0; j < nidx; j++)
        {
            scanf("%s", name);
            id = getid(name);
            v[id].push_back(idx);
        }
    }
    for (int i = 0; i < n; i++)
    {
        scanf("%s", name);
        id = getid(name);
        sort(v[id].begin(), v[id].end());
        printf("%s %lu", name, v[id].size());
        for (int j = 0; j < v[id].size(); j++)
        {
            printf(" %d", v[id][j]);
        }
        printf("\n");
    }
    return 0;
}
